package com.test.thread;

import java.util.concurrent.Semaphore;

public class Leet1117 {
}

/**
 * h每获取一次释放一个o许可，o每次获取两个许可（即2次h后执行一次o）
 * 对acquire和release的理解学到了==========
 */
class H2O {

	private Semaphore h = new Semaphore(2);
	private Semaphore o = new Semaphore(0);

	public H2O() {
	}

	public void hydrogen(Runnable releaseHydrogen) throws InterruptedException {
		h.acquire();
		// releaseHydrogen.run() outputs "H". Do not change or remove this line.
		releaseHydrogen.run();
		o.release();
	}

	public void oxygen(Runnable releaseOxygen) throws InterruptedException {
		o.acquire(2);
		// releaseOxygen.run() outputs "O". Do not change or remove this line.
		releaseOxygen.run();
		h.release(2);
	}
}

